--- title: "矩阵求和" created: 2025-11-28 tags: - 算法 --- # 矩阵求和 ## 题目 [矩阵求和](https://www.lanqiao.cn/paper/3851/problem/228/) ![[image-6ff56a07.png]] ## 思路分析 能想到的就是暴力+二维前缀和做 n太大了 静态数组 二维的1e7+10没运行就直接炸了 所以采用vector根据输入的n分配空间 能过几个是几个 最后是过了三 得10分 ![[image-6255e5ed.png]] ![[image-d58c34a0.png]] ## 代码实现 ```typescript #include using namespace std; #define endl '\n' int n; int main() { ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); cin>>n; vector> a(n + 1, vector(n + 1)); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ int x=__gcd(i,j); x*=x; a[i][j]=a[i-1][j]+a[i][j-1]-a[i-1][j-1]+x; } } cout<